package com.zd.lib.utils;

import android.text.TextUtils;

import java.security.MessageDigest;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;


public class ApiKeyUtils {


	private static final String NB = "NB";

	public static String md5(String sourceStr) {
        String result = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(sourceStr.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            result = buf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

	/**
	 * 获取请求的唯一标识：UUID + time
	 */
	public static String getNB() {
		String nb = SPUtils.getStringFromSP(NB, "");
		if ("".equals(nb)) {
			nb = md5(getUUID() + System.currentTimeMillis());
			SPUtils.setString2SP(NB, nb);
		}
		return nb;
	}

	private static String getUUID() {
		return UUID.randomUUID().toString();
	}



}
